const express=require('express');
const router=express.Router();
var svgCaptcha=require("svg-captcha");
const {userLogin,userImg,updateImg,newRegis,userInfo,userAllInfo}=require('../service/userService')
var codeConfig = {
  size: 4,// 验证码长度 // 验证码字符中排除 0o1i
  noise: 1, // 干扰线条的数量
  height: 44 
}


router.get('/register',async function(req,res,next){
  var captcha=svgCaptcha.create(codeConfig);
  req.session.captcha=captcha.text.toLocaleLowerCase();
  res.send(captcha.data);
})

router.post('/login',async function(req,res,next){
 if(req.session.captcha===req.body.code.toLocaleLowerCase()){
   delete req.body.code;
   let data=await userLogin(req.body);
   req.session[data.data._id]=data.data._id;
   res.send(data);
 }else{
   res.send({message:'验证码不对',status:'0'})
 }
})

router.post('/session',function(req,res,next){
  if(req.session[req.body._id]){
    let id=req.session[req.body._id];
    res.send({status:'200',_id:id});
  }else{
    res.send({status:'0'});
  }
})

router.post('/userImg',async function(req,res,next){
  let data = await userImg(req.body);
  res.send(data);
})

router.post('/updateImg',async function(req,res,next){
  let data = await updateImg(req.body);
})

router.post('/newRegis',async function(req,res,next){
  let data = await newRegis(req.body);
  res.send(data);
})

router.post('/userInfo',async function(req,res,next){
  let data= await userInfo(req.body);
  res.send(data);
})

router.post('/logout',async function(req,res,next){
  console.log(req.body);
  delete req.session[req.body._id];
  res.send();
})

router.get('/userAllInfo',async function(req,res,next){
  let data= await userAllInfo();
  res.send(data);
})

router.post('/userState',async function(req,res,next){
  if(req.session[req.body.id]){
    res.send(true);
  }else{
    res.send(false);
  }
})
module.exports=router;